package service

import (
	"errors"
	"strings"
	"time"

	"gitee.com/sansaniot/ssiot-core/facade/env"
	"gitee.com/sansaniot/ssiot-core/httpmvc/service"

	"ssalarm/common/constant"
	"ssalarm/common/dto"
	"ssalarm/common/utils"
	"ssalarm/internal/alarm/models"
	"ssalarm/internal/alarm/models/command"
	"ssalarm/internal/alarm/models/query"
)

type SysAlarm struct {
	sysAlarmModel *models.SysAlarm
	service.Service
}

func (d *SysAlarm) SetSysAlarmModel(m *models.SysAlarm) *SysAlarm {
	d.sysAlarmModel = m
	return d
}

func (d *SysAlarm) ProcessAlarm(req *command.AlarmIdReq) (err error) {
	if len(req.Id) == 0 {
		return errors.New("请选择告警记录")
	}
	alarmList := make([]models.SysAlarm, 0)
	where := dto.MakeCondition(query.SysAlarmQuery{IdIn: strings.Split(strings.ReplaceAll(req.Id, " ", ""), ",")})

	//查询要处理的告警记录
	if err = d.sysAlarmModel.FindList(&alarmList, where); err != nil {
		return
	}

	//更新redis状态
	for _, alarm := range alarmList {
		flag := false
		sn := alarm.DevSn
		variable := alarm.VarKey
		redisKey := constant.AlarmConfigKey + sn
		vb, _ := env.Cache.HashGet(redisKey, variable)
		if len(vb) > 0 {
			confArray := []map[string]interface{}{}
			utils.ConvertJsonToMap(vb, &confArray)
			for _, confJson := range confArray {
				triggerType := confJson["triggerType"].(float64)
				tmpCount := float64(0)
				if _, ok := confJson["tmpCount"]; ok {
					tmpCount = confJson["tmpCount"].(float64)
				}
				if triggerType == 1 && tmpCount > 0 {
					confJson["tmpCount"] = 0
					flag = true
				}
			}
			if flag {
				utils.ConvertMapToJson(confArray, &vb)
			}
		}
		if flag {
			_ = env.Cache.HashSet(redisKey, variable, vb)
		}
	}
	if err = d.sysAlarmModel.ModMap(d.sysAlarmModel, map[string]interface{}{"process_status": true, "update_time": time.Now(), "suggestion": req.Suggestion}, where); err != nil {
		return
	}
	return
}

func (d *SysAlarm) RemoveAlarmRecord(req *command.AlarmRemoveReq) (err error) {
	delIdList := strings.Split(strings.ReplaceAll(req.Id, " ", ""), ",")
	alarmList := make([]models.SysAlarm, 0)
	where := dto.MakeCondition(query.SysAlarmQuery{IdIn: delIdList})

	//查询要处理的告警记录
	if err = d.sysAlarmModel.FindList(&alarmList, where); err != nil {
		return
	}

	//更新redis状态
	for _, alarm := range alarmList {
		flag := false
		sn := alarm.DevSn
		variable := alarm.VarKey
		redisKey := constant.AlarmConfigKey + sn
		vb, _ := env.Cache.HashGet(redisKey, variable)
		if len(vb) > 0 {
			confArray := []map[string]interface{}{}
			utils.ConvertJsonToMap(vb, &confArray)
			for _, confJson := range confArray {
				triggerType := confJson["triggerType"].(float64)
				tmpCount := float64(0)
				if _, ok := confJson["tmpCount"]; ok {
					tmpCount = confJson["tmpCount"].(float64)
				}
				if triggerType == 1 && tmpCount > 0 {
					confJson["tmpCount"] = 0
					flag = true
				}
			}
			if flag {
				utils.ConvertMapToJson(confArray, &vb)
			}
		}
		if flag {
			_ = env.Cache.HashSet(redisKey, variable, vb)
		}
	}
	if err = d.sysAlarmModel.Delete(d.sysAlarmModel, where); err != nil {
		return
	}
	return
}
